﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Concrete;

namespace SportsStore.Domain
{
    /// <summary>
    /// This class represent the concrete implementation to access the data store using Entity Framework.
    /// </summary>
    public class EFProductRepository : IProductRepository
    {
        private EFDbContext _context = new EFDbContext();
 
        public IQueryable<Entities.Product> Products
        {
            get { return _context.Products; }
        }

        public void SaveProduct(Entities.Product product)
        {
            if (product.ProductID == 0)
            {
                _context.Products.Add(product);
                _context.Entry(product).State = System.Data.EntityState.Added;
            }
            else
            {
                _context.Entry(product).State = System.Data.EntityState.Modified;
            }

            _context.SaveChanges();
        }

        public void DeleteProduct(Entities.Product product)
        {
            _context.Products.Remove(product);
            _context.SaveChanges();
        }
    }
}
